package com.fishing.controller;

import QL.*;
import com.alibaba.fastjson.JSONObject;
import com.fishing.EnumEntity.Platformreceiving;
import com.fishing.util.personal.QLClientServer;
import com.fishing.util.personal.resource_log_reason_type;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/***
 * 首页
 * @author lrz
 *
 */
@Controller
public class StatController{
	
	
	//region
	@RequestMapping(value = "stat")
	public ModelAndView stat( HttpServletRequest request)throws Exception{
    	ModelAndView mv = new ModelAndView("stat");
    	return mv;
	}
	//endregion
	
	//region
	@RequestMapping(value = "stat2")
	public ModelAndView stat2( HttpServletRequest request)throws Exception{
    	ModelAndView mv = new ModelAndView("stat2");
    	return mv;
	}
	//endregion
	
	/**
	 * 针对多个表格请求数据  设计并发  synchronized修饰方法
	 * synchronized
	 * */
	//region
	@ResponseBody
	@RequestMapping(value="stat/loadStat",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	public  Map<String,Object> loadStat(HttpServletRequest request,HttpServletResponse response,String kind)throws Exception{
		List list  = new ArrayList();
		ButlerGetGlobalResourceStatRequest req = new ButlerGetGlobalResourceStatRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		ButlerGetGlobalResourceStatResponse rsp = new ButlerGetGlobalResourceStatResponse();
		QLClientServer.server(req.ZoneId);
		QLClient.Execute(req, rsp);
		if(rsp.StockItems != null){
			
		//循环资源存量列表
		for(ButlerResourceStockItem stockitem : rsp.StockItems) {
			long TotalGain = 0;
			long TotalCost = 0;
			long daojupingtaiGain = 0;
			//匹配资源存量->资源统计列表
				//resource_log_reason reason = new resource_log_reason();
				Map<String,Object> map = new HashMap<String,Object>();
				for(ButlerResourceStatItem statitem:rsp.StatItems) {
					map.put("Name", stockitem.Name);
					//资源存量名称 和 统计列表资源的名称相同  进入
					if(stockitem.Name.equals(statitem.Name)) {
						if(statitem.TotalCount > 0) {
							TotalGain += statitem.TotalCount;
						}else {
							TotalCost += -statitem.TotalCount;
						}
						for(resource_log_reason_type reason : EnumSet.allOf(resource_log_reason_type.class)){
							if(reason.getValue() == (int)statitem.Reason) {
								map.put(reason.name(), statitem.TotalCount);
								//平台内获得道具(数据统计)
								for(Platformreceiving reason2 : EnumSet.allOf(Platformreceiving.class)){
									if(reason.name().equals(reason2.name())) {
										daojupingtaiGain += statitem.TotalCount;
										break;
									}
								}
								break;
							}
						}
					}
				}
				map.put("TotalGain", TotalGain);
				map.put("TotalCost", TotalCost);
				map.put("CurrentStock",stockitem.Count);
				map.put("daojupingtaiGain", daojupingtaiGain);
				if(stockitem.Name.equals("金币")) {
					for(int i = 0;i < rsp.GamePlatformItems.size();i++) {
						if(rsp.GamePlatformItems.get(i).GameFlag == 1) {
							//捕鱼3D放水额度
							map.put("ThreeFishingWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
							//捕鱼3D抽水额度
							map.put("ThreeFishingWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
							//捕鱼3D平台收益
							map.put("ThreeFishingFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
							//捕鱼3D水位 
							map.put("ThreeFishingWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
						}else if(rsp.GamePlatformItems.get(i).GameFlag == 3){
							//水浒传放水额度
							map.put("ShzWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
							//水浒传抽水额度
							map.put("ShzWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
							//水浒传平台收益
							map.put("ShzFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
							//水浒传水位 
							map.put("ShzWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
						}else if(rsp.GamePlatformItems.get(i).GameFlag == 4) {
							//拉霸机放水额度
							map.put("LbjWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
							//拉霸机抽水额度
							map.put("LbjWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
							//拉霸机平台收益
							map.put("LbjFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
							//拉霸机水位 
							map.put("LbjWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
							//拉霸机彩金池
							map.put("LbjBonus", rsp.GamePlatformItems.get(i).Bonus);
							//总抽彩
							map.put("LbjBonusPull", rsp.GamePlatformItems.get(i).BonusPull);
							//总放彩
							map.put("LbjBonusPull", rsp.GamePlatformItems.get(i).BonusPush);
						}else if(rsp.GamePlatformItems.get(i).GameFlag == 5) {
							//打地鼠放水额度
							map.put("DdsWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
							//打地鼠抽水额度
							map.put("DdsWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
							//打地鼠平台收益
							map.put("DdsFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
							//打地鼠水位 
							map.put("DdsWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
							//打地鼠彩金池
							map.put("DdsBonus", rsp.GamePlatformItems.get(i).Bonus);
							//打地鼠总抽彩
							map.put("DdsBonusPull", rsp.GamePlatformItems.get(i).BonusPull);
							//打地鼠总放彩
							map.put("DdsBonusPull", rsp.GamePlatformItems.get(i).BonusPush);
						}else if(rsp.GamePlatformItems.get(i).GameFlag == 6) {
							//红包放水额度
							map.put("HBWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
							//红包抽水额度
							map.put("HBWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
							//红包平台收益
							map.put("HBFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
							//红包水位 
							map.put("HBWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
						}else if(rsp.GamePlatformItems.get(i).GameFlag == 7) {
							//龙虎斗水额度
							map.put("LhdWaterLinePush", rsp.GamePlatformItems.get(i).WaterLinePush);
							//龙虎斗抽水额度
							map.put("LhdWaterLinePull", rsp.GamePlatformItems.get(i).WaterLinePull);
							//龙虎斗平台收益
							map.put("LhdFeeGrab", rsp.GamePlatformItems.get(i).FeeGrab);
							//龙虎斗水位 
							map.put("LhdWaterLine", rsp.GamePlatformItems.get(i).WaterLine);
						}
					}
				}
				
			list.add(map);
			}
			Map<String,Object> map_data = new  HashMap<String,Object>();
			map_data.put("data", list);
			map_data.put("count", list.size());
			map_data.put("message", "");
			map_data.put("status", "200");
			
			return map_data;
		}else {
			Map<String,Object> map = new  HashMap<String,Object>();
			map.put("data", "");
			map.put("count", 0);
			map.put("message", "");
			map.put("status", "200");
			return map;
		}
	}
	//endregion
	
	//region
	@ResponseBody
	@RequestMapping(value="stat/loadStat2",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	public JSONObject loadStat2(HttpServletRequest request,HttpServletResponse response)throws Exception{
		List list  = new ArrayList();
		ButlerGetGlobalResourceStatRequest req = new ButlerGetGlobalResourceStatRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		ButlerGetGlobalResourceStatResponse rsp = new ButlerGetGlobalResourceStatResponse();
		QLClientServer.server(req.ZoneId);
		QLClient.Execute(req, rsp);
		if(rsp.StockItems != null){
			for(ButlerResourceStockItem stockitem : rsp.StockItems) {
				Map<String,Object> map = new HashMap<String,Object>();
				long GainByRecharge = 0;
				long GainByFishingKill = 0;
				long GainByFishingBonus = 0;
				long CostByFishingShoot = 0;
				long TotalGain = 0;
				long TotalCost = 0;
				long GainByOther = 0;
				for(ButlerResourceStatItem statitem:rsp.StatItems) {
					if(stockitem.Name.equals(statitem.Name) && statitem.TotalCount != 0) {
						if(statitem.TotalCount > 0) {
							TotalGain += statitem.TotalCount;
						}else {
							TotalCost += -statitem.TotalCount;
						}
						switch((int)statitem.Reason) { //switch不能有long类型
						//充值
						case 3:
							GainByRecharge += statitem.TotalCount;
							break;
						case 24:
							GainByRecharge += statitem.TotalCount;
							break;
						case 28:
							GainByRecharge += statitem.TotalCount;
							break;
						case 33:
							GainByRecharge += statitem.TotalCount;
							break;
						case 35:
							GainByRecharge += statitem.TotalCount;
							break;
							
							//捕鱼掉落获得
						case 301:
							GainByFishingKill += statitem.TotalCount;
							break;
							
							//奖金鱼抽奖获得
						case 304:
							GainByFishingBonus += statitem.TotalCount;
							break;
							
							//捕鱼开炮消耗
						case 300:
							CostByFishingShoot += -statitem.TotalCount;
							break;
						}
					}
				}
				//其他玩法获得
				GainByOther = TotalGain - GainByRecharge - GainByFishingKill - GainByFishingBonus;
				map.put("Name", stockitem.Name);
				map.put("TotalGain", TotalGain);
				map.put("TotalCost", TotalCost);
				map.put("GainByRecharge", GainByRecharge);
				map.put("GainByFishingKill", GainByFishingKill);
				map.put("GainByFishingBonus", GainByFishingBonus);
				map.put("GainByOther", GainByOther);
				map.put("CostByFishingShoot", CostByFishingShoot);
				map.put("CurrentCount", stockitem.Count);
				list.add(map);
			}
			Map<String,Object> map = new  HashMap<String,Object>();
			map.put("data", list);
			com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(map);
			return json;
		}else {
			Map<String,Object> map = new  HashMap<String,Object>();
			map.put("data", "");
			map.put("count", 0);
			map.put("message", "");
			map.put("status", "200");
			com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(map);
			return json;
		}
	}
    //endregion


}
